import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout/index.vue'
import Login from '../views/Login/Login.vue'

const Home = ()=>import('../views/Home/index.vue')
const Student = ()=>import('../views/Student/index.vue')
const Goods = ()=>import('../views/Goods/Goods.vue')
const Teach = ()=>import('../views/Teach/index.vue')
const Advert = ()=>import('../views/Advert/Advert.vue')
const Order = ()=>import('../views/Order/index.vue')
const OrderList = ()=>import('../views/Order/OrderList/index.vue')
const OrderBack = ()=>import('../views/Order/OrderBack/index.vue')
const Leave = ()=>import('../views/Leave/index.vue')
const Application = ()=>import('../views/Leave/Application/index.vue')
const Approval = ()=>import('../views/Leave/Approval/index.vue')



//子集路由

Vue.use(VueRouter)

const routes = [
  {
      path:'',
      component:Layout,
      meta:{
        isLogin:true
      },
      children:[
      {
        path:'/',
        name:'Home',
        component:Home
      },
      {
        path:'/student',
        name:'Student',
        component:Student
      },
      {
      path:'/goods',
      name:'Goods',
      component:Goods
    },
    {
      path:'/teach',
      name:'Teach',
      component:Teach
    },{
      path:'/advert',
      name:'Advert',
      component:Advert
    },{
      path:'/order',
      name:'Order',
      component:Order,
      redirect:'/order/order-list',  
      children:[
        {
          path:'order-list',
          component:OrderList
        },
        {
          path:'order-back',
          component:OrderBack
        }
      ],
    
    },{
      path:'/leave',
      name:'Leave',
      component:Leave,
      redirect:'/leave/leave-application',  
      children:[
        {
          path:'leave-application',
          component:Application
        },
        {
          path:'leave-approval',
          component:Approval
        }
      ],
    }
  ]

   },
   {
     path:'/login',
     name:'Login',
     component:Login
   }
  
]

const router = new VueRouter({
  routes
})
//获取vuex数据
import store from '../store/index';
//路由拦截
router.beforeEach((to,from,next)=>{
//判断是否需要登录
  if(to.matched.some(ele=>ele.meta.isLogin)){
    //判断用户是否已经登录
    let token =store.state.loginModule.userinfo.token;
    if(token){
      next()
    }else{
      next('/login')
    }
  }else{  //不需要登陆
    next();
  }
})

export default router
